package com.ruoyi.system.service.impl;

import java.util.List;

import com.ruoyi.common.utils.Cascader;
import com.ruoyi.common.utils.uuid.IdUtils;
import com.ruoyi.paper.domain.QtPaper;
import com.ruoyi.paper.domain.QtPaperTkNums;
import com.ruoyi.paper.mapper.QtPaperMapper;
import com.ruoyi.system.domain.*;
import com.ruoyi.system.mapper.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.system.service.IQtTkService;

/**
 * 题库管理Service业务层处理
 * 
 * @author zqq
 * @date 2021-11-16
 */
@Service
public class QtTkServiceImpl implements IQtTkService 
{
    @Autowired
    private QtTkMapper qtTkMapper;
    @Autowired
    private QtTypeMapper qtTypeMapper;
    @Autowired
    private QtChapterMapper qtChapterMapper;
    @Autowired
    private QtCourseMapper qtCourseMapper;
    @Autowired
    private QtOptionMapper qtOptionMapper;
    @Autowired
    private QtPaperMapper qtPaperMapper;
    /**
     * 查询题库管理
     * 
     * @param tkId 题库管理主键
     * @return 题库管理
     */
    @Override
    public QtTk selectQtTkByTkId(String tkId)
    {
        return qtTkMapper.selectQtTkByTkId(tkId);
    }

    /**
     * 查询题库管理列表
     * 
     * @param qtTk 题库管理
     * @return 题库管理
     */
    @Override
    public List<QtTk> selectQtTkList(QtTk qtTk)
    {
        List<QtTk> list=qtTkMapper.selectQtTkList(qtTk);

        return list;
    }

    /**
     * 新增题库管理
     * 
     * @param qtTk 题库管理
     * @return 结果
     */
    @Transactional
    @Override
    public int insertQtTk(QtTk qtTk)
    {
        String id=IdUtils.randomUUID();
        qtTk.setTkId(id);
        int rows = qtTkMapper.insertQtTk(qtTk);
        for (QtOption item:qtTk.getOptions()) {
            if(item.getOptionName().length()>2){
                item.setOptionId(IdUtils.randomUUID());
                item.setTkId(id);
                qtOptionMapper.insertQtOption(item);
            }
        }
       // insertQtChapter(qtTk);
        return rows;
    }

    /**
     * 修改题库管理
     * 
     * @param qtTk 题库管理
     * @return 结果
     */
    @Transactional
    @Override
    public int updateQtTk(QtTk qtTk)
    {
        qtOptionMapper.deleteQtOptionByOptionId(qtTk.getTkId());
        String id=qtTk.getTkId();
        for (QtOption item:qtTk.getOptions()) {
            item.setOptionId(IdUtils.randomUUID());
            item.setTkId(id);
            qtOptionMapper.insertQtOption(item);
        }
        return qtTkMapper.updateQtTk(qtTk);
    }

    /**
     * 批量删除题库管理
     * 
     * @param tkIds 需要删除的题库管理主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteQtTkByTkIds(String[] tkIds)
    {
        qtTkMapper.deleteQtChapterByChapterIds(tkIds);
        return qtTkMapper.deleteQtTkByTkIds(tkIds);
    }

    /**
     * 删除题库管理信息
     * 
     * @param tkId 题库管理主键
     * @return 结果
     */
    @Override
    public int deleteQtTkByTkId(String tkId)
    {
        qtTkMapper.deleteQtChapterByChapterId(tkId);
        return qtTkMapper.deleteQtTkByTkId(tkId);
    }

    @Override
    public List<Cascader> getCascader() {
        List<QtType> types=qtTypeMapper.selectQtTypeList(new QtType());
        List<QtChapter> chapters=qtChapterMapper.selectQtChapterList(new QtChapter());
        List<QtCourse> courses=qtCourseMapper.selectQtCourseList(new QtCourse());
        List<Cascader> res=new ArrayList<>();
        for (QtType type :types) {
            Cascader item=new Cascader();
            item.setLabel(type.getTypeName());
            item.setValue(type.getTypeId());
            List<Cascader> res1=new ArrayList<>();
            for (QtCourse course :courses) {
                if(type.getTypeId().equals(course.getTypeId())){
                    Cascader item1=new Cascader();
                    item1.setLabel(course.getCourseName());
                    item1.setValue(course.getCourseId());
                    List<Cascader> res2=new ArrayList<>();
                    for (QtChapter chapter :chapters) {
                        if(chapter.getCourseId().equals(course.getCourseId())){
                            Cascader item2=new Cascader();
                            item2.setLabel(chapter.getChapterName());
                            item2.setValue(chapter.getChapterId());
                            res2.add(item2);
                        }
                    }
                    item1.setChildren(res2);
                    res1.add(item1);
                }
            }
            item.setChildren(res1);
            res.add(item);
        }

        return res;
    }

    @Override
    public List<Cascader> getCascader2() {
        List<QtType> types=qtTypeMapper.selectQtTypeList(new QtType());
        List<QtCourse> courses=qtCourseMapper.selectQtCourseList(new QtCourse());
        List<Cascader> res=new ArrayList<>();
        for (QtType type :types) {
            Cascader item=new Cascader();
            item.setLabel(type.getTypeName());
            item.setValue(type.getTypeId());
            List<Cascader> res1=new ArrayList<>();
            for (QtCourse course :courses) {
                if(type.getTypeId().equals(course.getTypeId())){
                    Cascader item1=new Cascader();
                    item1.setLabel(course.getCourseName());
                    item1.setValue(course.getCourseId());

                    res1.add(item1);
                }
            }
            item.setChildren(res1);
            res.add(item);
        }

        return res;
    }

    @Override
    public List<QtPaperTkNums> getnums(QtTk qtTk) {
        if(qtTk.getChapterId()!=null && !qtTk.getChapterId().equals("")){
            String[] ary=qtTk.getChapterId().split(",");
            String res="";
            for(int i=0; i<ary.length; i++){
                res += "'"+ary[i]+"',";
            }
            res=res.substring(0, res.length()-1);
            qtTk.setChapterId(res);
            qtTk.setChapterId("("+qtTk.getChapterId()+")");
        }
        return qtTkMapper.getnums(qtTk);
    }

    @Override
    public List<String> getTkId(QtPaper qtPaper) {
        return qtTkMapper.getTkId(qtPaper);
    }

    /**
     * 为考试获取课程试卷级联数据
     * @return
     */
    @Override
    public List<Cascader> getCascaderByPaper() {
        List<QtType> types=qtTypeMapper.selectQtTypeList(new QtType());
        List<QtCourse> courses=qtCourseMapper.selectQtCourseList(new QtCourse());
        List<QtPaper> papers= qtPaperMapper.selectQtPaperList(new QtPaper());
        List<Cascader> res=new ArrayList<>();
        for (QtType type :types) {
            Cascader item=new Cascader();
            item.setLabel(type.getTypeName());
            item.setValue(type.getTypeId());
            List<Cascader> res1=new ArrayList<>();
            for (QtCourse course :courses) {
                if(type.getTypeId().equals(course.getTypeId())){
                    Cascader item1=new Cascader();
                    item1.setLabel(course.getCourseName());
                    item1.setValue(course.getCourseId());
                    List<Cascader> res2=new ArrayList<>();
                    for(QtPaper item2:papers){
                        if(item2.getCourseId().equals(course.getCourseId())){
                            Cascader item3=new Cascader();
                            item3.setLabel(item2.getPaperName());
                            item3.setValue(item2.getPaperId());
                            res2.add(item3);
                        }
                    }
                    item1.setChildren(res2);
                    res1.add(item1);
                }
            }
            item.setChildren(res1);
            res.add(item);
        }

        return res;
    }

    /**
     * 新增章节管理信息
     * 
     * @param qtTk 题库管理对象
     */
    public void insertQtChapter(QtTk qtTk)
    {
        List<QtChapter> qtChapterList = qtTk.getQtChapterList();
        String tkId = qtTk.getTkId();
        if (StringUtils.isNotNull(qtChapterList))
        {
            List<QtChapter> list = new ArrayList<QtChapter>();
            for (QtChapter qtChapter : qtChapterList)
            {
                qtChapter.setChapterId(tkId);
                list.add(qtChapter);
            }
            if (list.size() > 0)
            {
                qtTkMapper.batchQtChapter(list);
            }
        }
    }
}
